<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>frameelementmethodtransport.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_xpc_frameelementmethodtransport.js.html">frameelementmethodtransport.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Contains the frame element method transport for cross-domain
<a name="line17"></a> * communication. It exploits the fact that FF lets a page in an
<a name="line18"></a> * iframe call a method on the iframe-element it is contained in, even if the
<a name="line19"></a> * containing page is from a different domain.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.net.xpc.FrameElementMethodTransport&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.net.xpc&#39;);
<a name="line27"></a>goog.require(&#39;goog.net.xpc.Transport&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Frame-element method transport.
<a name="line32"></a> *
<a name="line33"></a> * Firefox allows a document within an iframe to call methods on the
<a name="line34"></a> * iframe-element added by the containing document.
<a name="line35"></a> * NOTE: Tested in all FF versions starting from 1.0
<a name="line36"></a> *
<a name="line37"></a> * @param {goog.net.xpc.CrossPageChannel} channel The channel this transport
<a name="line38"></a> *     belongs to.
<a name="line39"></a> * @constructor
<a name="line40"></a> * @extends {goog.net.xpc.Transport}
<a name="line41"></a> */
<a name="line42"></a>goog.net.xpc.FrameElementMethodTransport = function(channel) {
<a name="line43"></a>  /**
<a name="line44"></a>   * The channel this transport belongs to.
<a name="line45"></a>   * @type {goog.net.xpc.CrossPageChannel}
<a name="line46"></a>   * @private
<a name="line47"></a>   */
<a name="line48"></a>  this.channel_ = channel;
<a name="line49"></a>
<a name="line50"></a>  // To transfer messages, this transport basically uses normal function calls,
<a name="line51"></a>  // which are synchronous. To avoid endless recursion, the delivery has to
<a name="line52"></a>  // be artificially made asynchronous.
<a name="line53"></a>
<a name="line54"></a>  /**
<a name="line55"></a>   * Array for queued messages.
<a name="line56"></a>   * @type {Array}
<a name="line57"></a>   * @private
<a name="line58"></a>   */
<a name="line59"></a>  this.queue_ = [];
<a name="line60"></a>
<a name="line61"></a>  /**
<a name="line62"></a>   * Callback function which wraps deliverQueued_.
<a name="line63"></a>   * @type {Function}
<a name="line64"></a>   * @private
<a name="line65"></a>   */
<a name="line66"></a>  this.deliverQueuedCb_ = goog.bind(this.deliverQueued_, this);
<a name="line67"></a>};
<a name="line68"></a>goog.inherits(goog.net.xpc.FrameElementMethodTransport, goog.net.xpc.Transport);
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * The transport type.
<a name="line73"></a> * @type {number}
<a name="line74"></a> * @protected
<a name="line75"></a> */
<a name="line76"></a>goog.net.xpc.FrameElementMethodTransport.prototype.transportType =
<a name="line77"></a>   goog.net.xpc.TransportTypes.FRAME_ELEMENT_METHOD;
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Flag used to enforce asynchronous messaging semantics.
<a name="line82"></a> * @type {boolean}
<a name="line83"></a> * @private
<a name="line84"></a> */
<a name="line85"></a>goog.net.xpc.FrameElementMethodTransport.prototype.recursive_ = false;
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Timer used to enforce asynchronous message delivery.
<a name="line90"></a> * @type {number}
<a name="line91"></a> * @private
<a name="line92"></a> */
<a name="line93"></a>goog.net.xpc.FrameElementMethodTransport.prototype.timer_ = 0;
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Holds the function to send messages to the peer
<a name="line98"></a> * (once it becomes available).
<a name="line99"></a> * @type {Function?}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.net.xpc.FrameElementMethodTransport.outgoing_ = null;
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Connect this transport.
<a name="line107"></a> */
<a name="line108"></a>goog.net.xpc.FrameElementMethodTransport.prototype.connect = function() {
<a name="line109"></a>  if (this.channel_.getRole() == goog.net.xpc.CrossPageChannel.Role.OUTER) {
<a name="line110"></a>    // get shortcut to iframe-element
<a name="line111"></a>    this.iframeElm_ = this.channel_.iframeElement_;
<a name="line112"></a>
<a name="line113"></a>    // add the gateway function to the iframe-element
<a name="line114"></a>    // (to be called by the peer)
<a name="line115"></a>    this.iframeElm_[&#39;XPC_toOuter&#39;] = goog.bind(this.incoming_, this);
<a name="line116"></a>
<a name="line117"></a>    // at this point we just have to wait for a notification from the peer...
<a name="line118"></a>
<a name="line119"></a>  } else {
<a name="line120"></a>    this.attemptSetup_();
<a name="line121"></a>  }
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Only used from within an iframe. Attempts to attach the method
<a name="line127"></a> * to be used for sending messages by the containing document. Has to
<a name="line128"></a> * wait until the containing document has finished. Therefore calls
<a name="line129"></a> * itself in a timeout if not successful.
<a name="line130"></a> * @private
<a name="line131"></a> */
<a name="line132"></a>goog.net.xpc.FrameElementMethodTransport.prototype.attemptSetup_ = function() {
<a name="line133"></a>  var retry = true;
<a name="line134"></a>  /** @preserveTry */
<a name="line135"></a>  try {
<a name="line136"></a>    if (!this.iframeElm_) {
<a name="line137"></a>      // throws security exception when called too early
<a name="line138"></a>      this.iframeElm_ = window.frameElement;
<a name="line139"></a>    }
<a name="line140"></a>    // check if iframe-element and the gateway-function to the
<a name="line141"></a>    // outer-frame are present
<a name="line142"></a>    // TODO Make sure the following code doesn&#39;t throw any exceptions
<a name="line143"></a>    if (this.iframeElm_ &amp;&amp; this.iframeElm_[&#39;XPC_toOuter&#39;]) {
<a name="line144"></a>      // get a reference to the gateway function
<a name="line145"></a>      this.outgoing_ = this.iframeElm_[&#39;XPC_toOuter&#39;];
<a name="line146"></a>      // attach the gateway function the other document will use
<a name="line147"></a>      this.iframeElm_[&#39;XPC_toOuter&#39;][&#39;XPC_toInner&#39;] =
<a name="line148"></a>          goog.bind(this.incoming_, this);
<a name="line149"></a>      // stop retrying
<a name="line150"></a>      retry = false;
<a name="line151"></a>      // notify outer frame
<a name="line152"></a>      this.send(goog.net.xpc.TRANSPORT_SERVICE_, goog.net.xpc.SETUP_ACK_);
<a name="line153"></a>      // notify channel that the transport is ready
<a name="line154"></a>      this.channel_.notifyConnected_();
<a name="line155"></a>    }
<a name="line156"></a>  }
<a name="line157"></a>  catch (e) {
<a name="line158"></a>    goog.net.xpc.logger.severe(
<a name="line159"></a>        &#39;exception caught while attempting setup: &#39; + e);
<a name="line160"></a>  }
<a name="line161"></a>  // retry necessary?
<a name="line162"></a>  if (retry) {
<a name="line163"></a>    if (!this.attemptSetupCb_) {
<a name="line164"></a>      this.attemptSetupCb_ = goog.bind(this.attemptSetup_, this);
<a name="line165"></a>    }
<a name="line166"></a>    window.setTimeout(this.attemptSetupCb_, 100);
<a name="line167"></a>  }
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Handles transport service messages.
<a name="line173"></a> * @param {string} payload The message content.
<a name="line174"></a> */
<a name="line175"></a>goog.net.xpc.FrameElementMethodTransport.prototype.transportServiceHandler =
<a name="line176"></a>    function(payload) {
<a name="line177"></a>  if (this.channel_.getRole() == goog.net.xpc.CrossPageChannel.Role.OUTER &amp;&amp;
<a name="line178"></a>      !this.channel_.isConnected() &amp;&amp; payload == goog.net.xpc.SETUP_ACK_) {
<a name="line179"></a>    // get a reference to the gateway function
<a name="line180"></a>    this.outgoing_ = this.iframeElm_[&#39;XPC_toOuter&#39;][&#39;XPC_toInner&#39;];
<a name="line181"></a>    // notify the channel we&#39;re ready
<a name="line182"></a>    this.channel_.notifyConnected_();
<a name="line183"></a>  } else {
<a name="line184"></a>    throw Error(&#39;Got unexpected transport message.&#39;);
<a name="line185"></a>  }
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Process incoming message.
<a name="line191"></a> * @param {string} serviceName The name of the service the message is to be
<a name="line192"></a> * delivered to.
<a name="line193"></a> * @param {string} payload The message to process.
<a name="line194"></a> * @private
<a name="line195"></a> */
<a name="line196"></a>goog.net.xpc.FrameElementMethodTransport.prototype.incoming_ =
<a name="line197"></a>    function(serviceName, payload) {
<a name="line198"></a>  if (!this.recursive_ &amp;&amp; this.queue_.length == 0) {
<a name="line199"></a>    this.channel_.deliver_(serviceName, payload);
<a name="line200"></a>  }
<a name="line201"></a>  else {
<a name="line202"></a>    this.queue_.push({serviceName: serviceName, payload: payload});
<a name="line203"></a>    if (this.queue_.length == 1) {
<a name="line204"></a>      this.timer_ = window.setTimeout(this.deliverQueuedCb_, 1);
<a name="line205"></a>    }
<a name="line206"></a>  }
<a name="line207"></a>};
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Delivers queued messages.
<a name="line212"></a> * @private
<a name="line213"></a> */
<a name="line214"></a>goog.net.xpc.FrameElementMethodTransport.prototype.deliverQueued_ =
<a name="line215"></a>    function() {
<a name="line216"></a>  while (this.queue_.length) {
<a name="line217"></a>    var msg = this.queue_.shift();
<a name="line218"></a>    this.channel_.deliver_(msg.serviceName, msg.payload);
<a name="line219"></a>  }
<a name="line220"></a>};
<a name="line221"></a>
<a name="line222"></a>
<a name="line223"></a>/**
<a name="line224"></a> * Send a message
<a name="line225"></a> * @param {string} service The name off the service the message is to be
<a name="line226"></a> * delivered to.
<a name="line227"></a> * @param {string} payload The message content.
<a name="line228"></a> */
<a name="line229"></a>goog.net.xpc.FrameElementMethodTransport.prototype.send =
<a name="line230"></a>    function(service, payload) {
<a name="line231"></a>  this.recursive_ = true;
<a name="line232"></a>  this.outgoing_(service, payload);
<a name="line233"></a>  this.recursive_ = false;
<a name="line234"></a>};
<a name="line235"></a>
<a name="line236"></a>
<a name="line237"></a>/**
<a name="line238"></a> * Disposes of the transport.
<a name="line239"></a> */
<a name="line240"></a>goog.net.xpc.FrameElementMethodTransport.prototype.disposeInternal =
<a name="line241"></a>    function() {
<a name="line242"></a>  goog.net.xpc.FrameElementMethodTransport.superClass_.disposeInternal.call(
<a name="line243"></a>      this);
<a name="line244"></a>  this.outgoing_ = null;
<a name="line245"></a>  this.iframeElm_ = null;
<a name="line246"></a>};
</pre>


</body>
</html>
